<template>
    <div class="app-container">
        <div v-show="!editRole">
            <el-row>
                <el-col :span="24" align="right">
                    <el-button type="text" @click="enterCreateRole" style="font-size: 17px;">新建角色</el-button>
                </el-col>
            </el-row>
            <el-table v-loading="listLoading" :data="viewData" element-loading-text="Loading" border highlight-current-row
                :cell-style="{ padding: '5px 0' }">
                <el-table-column label="编号" align="center" :min-width="'10%'" :max-width="'10%'">
                    <template slot-scope="scope">
                        {{ scope.row.roleId }}
                    </template>
                </el-table-column>
                <el-table-column label="角色名" align="center" :min-width="'10%'" :max-width="'10%'">
                    <template slot-scope="scope">
                        {{ scope.row.roleName }}
                    </template>
                </el-table-column>
                <el-table-column label="角色描述" align="center" :min-width="'60%'" :max-width="'60%'">
                    <template slot-scope="scope">
                        {{ scope.row.roleDescription }}
                    </template>
                </el-table-column>
                <el-table-column fixed="right" align="center" label="操作" :min-width="'20%'" :max-width="'20%'">
                    <template slot-scope="scope">
                        <el-button @click="enterEditRole(scope.row)" type="text"
                            :disabled="scope.row.roleId <= 3">编辑</el-button>
                        <el-button @click="deleteRole(scope.row)" type="text"
                            :disabled="scope.row.roleId <= 3">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination @size-change="changePageSize" @current-change="changeCurrentPage"
                :current-page="page.currentPage" :page-sizes="[10, 20, 50]" :page-size="page.pageSize"
                layout="total, sizes, prev, pager, next, jumper" :total="page.totalItems">
            </el-pagination>
        </div>
        <div v-if="editRole">
            <!-- <el-button size="medium" @click="editRole = false">返回</el-button> -->
            <createRole :role="role" @update-parameter="() => { editRole = false, this.fetchData() }" />
        </div>
    </div>
</template>

<script>
import { getAllRoles, deleteRole } from '@/api/province/roleManage'
import createRole from './createRole.vue';

export default {
    data() {
        return {
            listLoading: false,
            rawData: [],
            viewData: [],
            page: {
                currentPage: 1,
                pageSize: 10,
                totalItems: 0,
            },
            editRole: false,
            role: {}
        }
    },
    components: {
        createRole
    },
    created() {
        this.fetchData();
    },
    methods: {
        display() {
            this.viewData = this.rawData.slice((this.page.currentPage - 1) * this.page.pageSize, Math.min(this.page.totalItems, this.page.currentPage * this.page.pageSize))
        },

        fetchData() {
            this.listLoading = true
            getAllRoles().then(response => {
                this.rawData = response.data
                this.page.totalItems = this.rawData.length
                this.display()
                this.listLoading = false
            })
        },

        changePageSize(val) {
            this.page.pageSize = val;
            this.display();
        },

        changeCurrentPage(val) {
            this.page.currentPage = val;
            this.display();
        },

        deleteRole(role) {
            this.$confirm('确定要删除该角色吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() =>
                deleteRole({ roleId: role.roleId }).then(response => {
                    this.$message({
                        message: '删除成功',
                        type: 'success'
                    })
                    this.fetchData()
                })
            ).catch(() =>
                this.$message({
                    type: 'info',
                    message: '已取消'
                })
            )
        },

        enterCreateRole() {
            this.role = {}
            this.editRole = true
        },

        enterEditRole(role) {
            console.log(role)
            this.role = role
            this.editRole = true
        },
    }
}
</script>